/*
 * Monster.java
 *
 * Created on January 6, 2008, 3:34 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class Monster {
     private dbConnection dbcon = new dbConnection();
     private int cHP, mHP, status, xC, yC, id;
     private String mName;
    /** Creates a new instance of Monster */
    public Monster() {
    }
    public Monster(int chp, int mhp, int mstatus, int xc, int yc, int mid, String mname) {
        setCHP(chp);
        setMHP(mhp);
        setStatus(mstatus);
        setXC(xc);
        setYC(yc);
        setId(mid);
        setMName(mname);
    }
    public int generateId ()
    {
         int num = 1;
        while (checkExist(num))
        {
            Random rand = new Random();
            num = rand.nextInt(100);
            System.out.println(num);
            
        }
        return num;
    }
    
    public boolean checkExist(int num) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM Monster WHERE id = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt(1, num);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
    
    public boolean checkMNameExist(String pName) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM Monster WHERE monsterName = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, pName);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
    
    public void addMonster (Monster m){
      	m.setId(m.generateId());					
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;
        if (!checkMNameExist(m.getMName()))
        {
            getDbcon().connect();

            query = "INSERT INTO Monster (monsterName, currentHP, maxHP, status, xcoordinate, ycoordinate, id) VALUES (?,?,?,?,?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, m.getMName());
                stmt.setInt(2, m.getCHP());
                stmt.setInt(3, m.getMHP());
                stmt.setInt(4, m.getStatus());
                stmt.setInt(5, m.getXC());
                stmt.setInt(6, m.getYC());
                stmt.setInt(7, m.getId());
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            getDbcon().disconnect();
        }
    }
    
    public void editHP (int id, int value)
   {
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        getDbcon().connect();
        query = "UPDATE Monster SET currentHP = ? WHERE id = ?";
        try 
        {
             
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt(1, value);
            stmt.setInt(2, id);
            stmt.executeUpdate();	
            stmt.close();					
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
   }
     public void editCoordinates (int id, int xValue, int yValue)
   {
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        getDbcon().connect();
        query = "UPDATE Monster SET xcoordinate = ?, ycoordinate = ? WHERE id = ?";
        try 
        {
             
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt(1, xValue);
            stmt.setInt(2, yValue);
            stmt.setInt(3, id);
            stmt.executeUpdate();	
            stmt.close();					
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
   }
      public void deleteMonster (int id) {
        		
    	String query;		
    	ResultSet rs;   
        PreparedStatement stmt;
        
       	getDbcon().connect();						
        query = "DELETE FROM Monster WHERE id = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);	
            stmt.setInt(1, id);
            stmt.executeUpdate();				
            stmt.close();						
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
      }
      
    public void viewMonster (int id){
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT monsterName, currentHP, maxHP, status, xcoordinate, ycoordinate, id FROM Monster WHERE id = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {
                this.setMName(rs.getString("monsterName"));
                this.setCHP(rs.getInt("currentHP"));
                this.setMHP(rs.getInt("maxHP"));
                this.setStatus(rs.getInt("status"));
                this.setXC(rs.getInt("xcoordinate"));
                this.setYC(rs.getInt("ycoordinate"));
                this.setId(rs.getInt("id"));
            }
            rs.close(); 					
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
 
        }
    }

    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public int getCHP() {
        return cHP;
    }

    public void setCHP(int cHP) {
        this.cHP = cHP;
    }

    public int getMHP() {
        return mHP;
    }

    public void setMHP(int mHP) {
        this.mHP = mHP;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getXC() {
        return xC;
    }

    public void setXC(int xC) {
        this.xC = xC;
    }

    public int getYC() {
        return yC;
    }

    public void setYC(int yC) {
        this.yC = yC;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMName() {
        return mName;
    }

    public void setMName(String mName) {
        this.mName = mName;
    }
}
